package com.unnamed.collisions.editor;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.FlowLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JButton;

import com.unnamed.collisions.geometry.Point;
import com.unnamed.collisions.geometry.Polygon;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PolyEdit {

	private JFrame frame;
	private PolyPanel polyPanel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PolyEdit window = new PolyEdit();
					window.frame.setVisible(true);
					window.frame.createBufferStrategy(2);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public PolyEdit() {
		initialize();
		
		this.polyPanel.startTimer();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 553, 450);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		frame.getContentPane().add(panel, BorderLayout.SOUTH);
		
		JLabel lbInfo = new JLabel("New label");
		lbInfo.setHorizontalAlignment(SwingConstants.LEFT);
		panel.add(lbInfo);
		
		JToolBar toolBar = new JToolBar();
		frame.getContentPane().add(toolBar, BorderLayout.NORTH);
		
		polyPanel = new PolyPanel();
		frame.getContentPane().add(polyPanel, BorderLayout.CENTER);
		
		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.WEST);
		
		JButton btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Polygon p = new Polygon(
					new Point[]{
							new Point(-1,-1),
							new Point(-1,1),
							new Point(1,1),
							new Point(1,-1)
					}
				);
				java.awt.Point origin = polyPanel.viewer.getOrigin();
				p.translate(origin.x, origin.y);
				p.scale(10, 10);
				polyPanel.viewer.addPolygon(p);
			}
		});
		panel_1.add(btnNewButton);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		frame.addComponentListener(new ComponentListener() {
			
			@Override
			public void componentShown(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void componentResized(ComponentEvent e) {
				// TODO Auto-generated method stub
				polyPanel.resize();
			}
			
			@Override
			public void componentMoved(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void componentHidden(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
	}

}
